import 'package:flutter/material.dart';
import './pages/home.dart';
import './pages/classify.dart';
import './pages/mine.dart';

class LayoutApp extends StatefulWidget {
  const LayoutApp({Key? key}) : super(key: key);

  @override
  _LayoutAppState createState() => _LayoutAppState();
}

class _LayoutAppState extends State<LayoutApp> {
  // 当前页面索引
  late int _currentIndex;
  // 底部tabBar
  late final List<Map> tabBarList;
  // pageView页面控制器
  late PageController _pageController;
  // 页面组件数组
  late List<Widget> pages = [];

  @override
  void initState() {
    tabBarList = [
      {'icon': Icons.home,  'title': '首页', 'topTile': 'vivo官方商城'},
      {'icon': Icons.widgets, 'title': '分类', 'topTile': '全部分类'},
      {'icon': Icons.account_circle, 'title': '我的', 'topTile': '个人中心'},
    ];
    pages..add(const Home())..add(const Classify())..add(const Mine());
    _currentIndex = 0;
    _pageController = PageController(initialPage: 0, keepPage: true);
    super.initState();
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          tabBarList[_currentIndex]['topTile'],
          style: const TextStyle(
            color: Colors.black,
            fontSize: 16.0
          ),
        ),
        toolbarHeight: 50.0,
        backgroundColor: Colors.white,
      ),
      // body: Center(child: Text(tabBarList[_currentIndex]['title']),),
      body: PageView(
        controller: _pageController,
        children: pages,
        onPageChanged: (int index){
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.white,
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        items: tabBarList.map((item){
          return BottomNavigationBarItem(
            icon: Icon(item['icon']),
            label: item['title'],
          );
        }).toList(),
        onTap: (int index){
          // 实现滑动点击联动
          _pageController.jumpToPage(index);
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }
}

